﻿using Cc.Util;
using Magic.Core;
using Magic.Core.Service;
using ManagementTools.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ManagementTools
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            btnLogout.Enabled = string.IsNullOrEmpty(App.Token);
        }
        /// <summary>
        /// 免登录后台，执行建表及代码生成等功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNoLogin_Click(object sender, EventArgs e)
        {
            MainForm frm = new MainForm();
            Hide();
            frm.ShowDialog();
            Show();
        }
        /// <summary>
        /// 登录后台，以管理员身份初始化系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            LoginInput input = new LoginInput()
            {
                TenantId = txtTenant.Text.ParseToInt(),
                Account = txtAccount.Text,
                Password = txtPassword.Text,
            };
            string url = App.ApiServer + "/login";
            var ans = HttpHelper.HttpPost(url, input);
            if(string.IsNullOrEmpty(ans))
            {
                MessageBox.Show("系统无响应","登录失败",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            else
            {
                var res = JsonConvert.DeserializeObject<HttpResponse>(ans);
                if(res != null)
                {
                    if(res.Success) //提取令牌
                    {
                        App.Token = "Bearer " + res.Data;
                        btnLogin.Enabled = false;
                        btnLogout.Enabled = true;
                        App.LoginUser = input;
                        this.Hide();
                        MainForm frm = new MainForm();
                        frm.ShowDialog();
                        this.Show();
                    }
                    else
                    {
                        MessageBox.Show(res.Message, "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("系统响应异常", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
           
        }

        private void btnLogout_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(App.Token)) {
                btnLogout.Enabled = false;
                return; }
            string url = App.ApiServer + "/logout";
            var ans = HttpHelper.HttpGet(url, 10,App.Token);
             if (string.IsNullOrEmpty(ans))
            {
                MessageBox.Show("系统无响应", "登出失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                
            }
            else
            {
                var res = JsonConvert.DeserializeObject<HttpResponse>(ans);
                if (res != null)
                {
                    if (res.Success) 
                    {
                        MessageBox.Show("登出成功", "登出失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        App.Token = "";
                        
                    }
                    else
                    {
                        MessageBox.Show(res.Message, "登出失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("系统响应异常", "登出失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            btnLogout.Enabled = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        
    }
}
